package com.dfxy.blog.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil {

    private static String dbUrl = "jdbc:mysql://localhost/dfxy_blog_shixun";
    private static String dbUserName = "root";
    private static String dbPassword = "root";
    private static String jdbcName = "com.mysql.cj.jdbc.Driver";

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(jdbcName);
        Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
        return con;
    }

    public static void closeConnection(Connection con) throws SQLException {
        if (con != null) {
            con.close();
        }
    }

    public static void main(String args[]){
        try {
            Connection con = DBUtil.getConnection();
            System.out.println("数据库连接成功！");
            DBUtil.closeConnection(con);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
